package mongodb

import (
	"context"
	"fmt"
	"hft/global"
	"os"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func InitMongoDB() *mongo.Client {
	fmt.Println("mongo", "mongodb://"+global.OC_CONFIG.MongoDB.User+":"+global.OC_CONFIG.MongoDB.Password+"@"+global.OC_CONFIG.MongoDB.Addr+":"+global.OC_CONFIG.MongoDB.Port+"/?authSource=admin")
	client := options.Client().ApplyURI("mongodb://" + global.OC_CONFIG.MongoDB.User + ":" + global.OC_CONFIG.MongoDB.Password + "@" + global.OC_CONFIG.MongoDB.Addr + ":" + global.OC_CONFIG.MongoDB.Port + "/?authSource=admin")

	MongoDBClient, err := mongo.Connect(context.TODO(), client)

	if err != nil {
		fmt.Println("mongodb:", err)
		os.Exit(0)
	}

	// ping通
	err = MongoDBClient.Ping(context.TODO(), nil)

	if err != nil {
		fmt.Println("mongodb-ping:", err)
		os.Exit(0)
	}

	fmt.Println("mongodb初始化成功")

	return MongoDBClient
}

func InitMongoDB1() *mongo.Client {
	client := options.Client().ApplyURI("mongodb://" + global.OC_CONFIG.MongoDB.User1 + ":" + global.OC_CONFIG.MongoDB.Password1 + "@" + global.OC_CONFIG.MongoDB.Addr1 + ":" + global.OC_CONFIG.MongoDB.Port1 + "/?authSource=admin&connect=direct")

	MongoDBClient, err := mongo.Connect(context.TODO(), client)

	if err != nil {
		fmt.Println("mongodb:", err)
		os.Exit(0)
	}

	// ping通
	err = MongoDBClient.Ping(context.TODO(), nil)

	if err != nil {
		fmt.Println("mongodb-ping:", err)
		os.Exit(0)
	}

	fmt.Println("mongodb1初始化成功")

	return MongoDBClient
}
